package com.lk.security;

import com.lk.web.entity.SysUser;
import com.lk.web.service.SysPermissionService;
import com.lk.web.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @program: lk-security-parent
 * @description: 自定义用户实现类->查询数据库中的用户信息
 * @author: Aspirin
 * @create: 2020-05-25 22:08
 * @remarks: AbstractUserDetailsService实现了UserDetailsService
 */
@Component("customUserDetailsService")
public class CustomUserDetailsServiceImpl extends AbstractUserDetailsService {
  private final Logger logger = LoggerFactory.getLogger(getClass());
  // 不能删除
  @Autowired PasswordEncoder passwordEncoder;
  @Autowired SysUserService sysUserService;
  @Autowired SysPermissionService sysPermissionService;

  @Override
  public SysUser findSysUser(String usernameOrMobile) {
    logger.info("请求认证的用户名：{}", usernameOrMobile);
    // 1.通过请求的用户名去数据库中查询用户信息
    SysUser sysUser = sysUserService.findByUsername(usernameOrMobile);
    if (sysUser == null) {
      throw new UsernameNotFoundException("用户名或密码错误");
    }
    return sysUser;
  }
}
